package com.swdegao.iot.netty.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import com.alibaba.fastjson.JSON;
import com.swdegao.iot.netty.entity.CallBackData;
import com.swdegao.iot.netty.entity.MyProps;
import com.swdegao.iot.netty.entity.Parameter;
import com.swdegao.iot.netty.entity.PushMessage;
import com.swdegao.iot.netty.entity.UpData;
import com.swdegao.iot.netty.service.impl.CMDServiceImpl;
/**
 * 
 * @author 只会走路的小鸟
 * 2020-11-05
 *
 */
/*@Component
@Repository
@SuppressWarnings({"unchecked","rawtypes"})*/
public class RocketMQUtils {
	//定义一个全局的记录器，通过LoggerFactory获取
    private final static Logger logger = LoggerFactory.getLogger(RocketMQUtils.class); 
	@Autowired
    private RocketMQTemplate rocketMQTemplate;
	@Autowired
    private MyProps myProps;
	
	public void sendMessage(String topic, Object data) {
        rocketMQTemplate.convertAndSend(topic, data);
    }

    public void sendMessage(String topic, String tags, Object data) {
        rocketMQTemplate.convertAndSend(String.format("%s:%s", topic, tags), data);
    }
    public void sendMessageOfKeys(String keys, String topic, Object data) {
    	Message message = MessageBuilder.withPayload(data).setHeader("KEYS", keys).build();
    	rocketMQTemplate.convertAndSend(topic, message);
    }
    public void sendMessage(String keys, String topic,String tags, Object data) {
    	Message message = MessageBuilder.withPayload(data).setHeader("KEYS", keys).build();
    	 //rocketMQTemplate.convertAndSend(String.format("%s:%s", topic, tags), message);
    	rocketMQTemplate.syncSend(String.format("%s:%s", topic, tags), message);
    }
    
    public PushMessage  getCallBackBean(CallBackData callBackData){
    	long nowtime = System.currentTimeMillis();
    	String imei = callBackData.getImei();
		PushMessage pusmessage = new PushMessage();
		pusmessage.setImei(imei);
		pusmessage.setServiceId("");
		pusmessage.setPushTime(nowtime);

		// 从msg中获取updata
		List<UpData> list = new ArrayList<UpData>();
		UpData updata = new UpData();
		updata.setCreatetime(callBackData.getCreatetime());
		updata.setImei(imei);

		// logger.info(imei+"###########################" +
		// Hex.encodeHexString(receiveMsgBytes));
		updata.setUpData(callBackData.getUpData());
		list.add(updata);
		pusmessage.setUpDataList(list);
		return pusmessage;
    }
	public void doSendHttpPost(CallBackData callBackData){
		try {
			String url = callBackData.getUrl();
			if(  "".equals(url))url = myProps.getMycallbackurl();
		    Pattern p = Pattern.compile(Parameter.regUrl);
		    Matcher m = p.matcher(url);
			if (!m.matches()) {
				logger.info("Url地址错误");
			}
			long nowtime = System.currentTimeMillis();
			String imei = callBackData.getImei();
			PushMessage pusmessage = new PushMessage();
			pusmessage.setImei(imei);
			pusmessage.setServiceId("");
			pusmessage.setPushTime(nowtime);

			// 从msg中获取updata
			List<UpData> list = new ArrayList<UpData>();
			UpData updata = new UpData();
			updata.setCreatetime(callBackData.getCreatetime());
			updata.setImei(imei);

			// logger.info(imei+"###########################" +
			// Hex.encodeHexString(receiveMsgBytes));
			updata.setUpData(callBackData.getUpData());
			list.add(updata);
			pusmessage.setUpDataList(list);
			// logger.error(imei+"Parametercall_back_url"+url);
			try {
				HttpClientUtil.sendHttpPostJson(url, JSON.toJSONString(pusmessage));
			} catch (Exception e) {
				logger.info("CallBackServiceImpl-doSendHttpPostC 推送异常："+e.getMessage());
			}

		}catch(Exception e) {
			logger.info("CallBackServiceImpl-doSendHttpPostC 推送异常："+e.getMessage());
		}
		

	}
   
}